using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SkillObject_Base : MonoBehaviour
{
    protected Player player;
    protected Animator animator;
    protected Rigidbody2D rb;
    protected DamageScaleData damageScaleData;
    protected ElementType currentElementType;
    protected AttackData attackData;

    [Header("----- Explode Check Details -----")]
    [SerializeField] private LayerMask whatIsEnemy;
    [SerializeField] protected Transform targetCheck;
    [SerializeField] protected float checkRadius;

    [Header("----- HitVFX Details -----")]
    [SerializeField] private GameObject hitVFX;

    protected Transform lastAttackTarget;
    protected virtual void Awake()
    {
        damageScaleData = new();
        rb = GetComponent<Rigidbody2D>();
        animator = GetComponentInChildren<Animator>();
    }
    protected virtual void OnEnable()
    {

    }
    protected virtual void OnDisable()
    {

    }
    protected virtual void Update()
    {

    }
    public Collider2D[] GetAllTargetEnemies(Transform t, float radius)
    {
        return Physics2D.OverlapCircleAll(t.position, radius, whatIsEnemy);
    }
    protected void DamageEnemiesInRedius(Transform t, float radius)
    {
        if (GetAllTargetEnemies(t, radius).Length == 0)
        {
            currentElementType = ElementType.None;
            return;
        }
        foreach (var target in GetAllTargetEnemies(t, radius))
        {
            IDamagable damagable = target.GetComponent<IDamagable>();
            if (damagable == null)
                continue;
            attackData = player.playerStats.GetAttackData(damageScaleData);
            Entity_StatHandler targetStatHandler = target.GetComponent<Entity_StatHandler>();
            float physicalDamage = attackData.physicalDamage;
            float elementalDamage = attackData.elementalDamage;
            ElementType elementType = attackData.elementType;
            damagable.TakeDamage(physicalDamage, elementalDamage, ElementType.None, player.transform);
            lastAttackTarget = target.transform;
            if (elementType != ElementType.None)
            {
                targetStatHandler?.ApplyStatusEffect(elementType, attackData.effectData);
            }

            GameObject explodeEffectPrefab = PoolManager.Instance.Release(hitVFX, (explodePrefab) =>
            {
                explodePrefab.transform.position = transform.position;
                explodePrefab.transform.rotation = Quaternion.identity;
            });

            currentElementType = elementType;
        }
    }
    protected Transform GetClosestTargetEnemy()
    {
        Transform target = null;
        float defaultDistance = float.MaxValue;
        foreach (var enemy in GetAllTargetEnemies(transform, 10))
        {
            float distance = (enemy.transform.position - transform.position).magnitude;
            if (distance < defaultDistance)
            {
                target = enemy.transform;
                defaultDistance = distance;
            }
        }
        return target;
    }

    public void OnDrawGizmos()
    {
        if (targetCheck == null)
            return;
        Gizmos.DrawWireSphere(targetCheck.position, checkRadius);
    }
    
}
